<?php

namespace App\Models;

use App\Http\Controllers\AccessSystem\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 图书到家书单推荐书籍表
 */
class BookHomeReadingListBook extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'book_home_reading_list_book';


    /**
     * 获取数据
     * @param except_reading_id 需要排除的书单id
     * @param type 类型 1 新书 2 馆藏书
     * @param keywords string 搜索关键词
     * @param shop_id string 书店id  如果是新书
     */
    public function getReadingListBookData($except_reading_id, $type = null, $shop_id = null, $keywords = null, $limit)
    {
        $data = [];
        switch ($type) {
            case 1: // 新书
                $commonObj = new CommonController();
                $shop_all_id = $commonObj->getAdminShopIdAll();
                if (!empty($shop_all_id)) {
                    $shopBookModel = new ShopBook();
                    $field = ['n.id', 'n.book_name', 'n.author', 'n.press', 'n.pre_time', 'n.isbn', 'n.price', 'n.img'];
                    $data = $shopBookModel->lists($field, $shop_id, $keywords, null, null, null, null,  null,  null, null, $shop_all_id, $except_reading_id, $limit);
                }
                break;
            case 2: // 馆藏书
                $libBookRecomModel = new LibBookRecom();
                $field = ['id', 'metaid', 'metatable', 'book_name', 'barcode', 'author', 'press', 'pre_time', 'type_id', 'isbn', 'book_num', 'price', 'intro', 'img'];
                $data = $libBookRecomModel->lists($field, null, null, $keywords, null, null, $except_reading_id, $limit);
                break;
            default:
                $data = [];
        }
        return $data;
    }


    /**
     * 获取书单书籍列表
     * @param limit int 分页大小
     * @param reading_id 书单id
     */
    public function lists($search_data = [])
    {
        $reading_id = $search_data['reading_id'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        //获取书单数据
        $bookHomeReadingListModel = new BookHomeReadingList();
        $reading_info = $bookHomeReadingListModel->detail($reading_id);
        if (empty($reading_info)) {
            return [];
        }
        if ($reading_info['type'] == 1) {
            $bookModel = new ShopBook();
            $field = ['r.id', 'r.reading_id', 'r.book_id', 'r.create_time', 'b.book_name', 'b.author', 'b.press', 'b.pre_time', 'b.isbn', 'b.price', 'b.img', 'b.book_num'];
        } else {
            $bookModel = new LibBookRecom();
            $field = ['r.id', 'r.reading_id', 'r.book_id', 'r.create_time', 'metaid', 'metatable', 'b.book_name', 'b.author', 'b.press', 'b.pre_time', 'b.isbn', 'b.price', 'b.img', 'b.book_num', 'b.barcode'];
        }
        $res = $this->from($this->getTable() . " as r")->select($field)
            ->join($bookModel->getTable() . ' as b', 'b.id', '=', 'r.book_id')
            ->where('reading_id', $reading_id)
            ->where('b.is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 添加系统类型作品
     * @param reading_id int 书单id
     * @param book_ids int 系统类型作品的id 多个逗号拼接
     */
    public function addBookHomeReadingListBook($reading_id, $book_ids)
    {
        $old_book_ids = $this->where('reading_id', $reading_id)->pluck('book_id')->toArray();
        $book_ids = explode(',', $book_ids);
        foreach ($book_ids as $key => $val) {
            if (!in_array($val, $old_book_ids)) {
                $obj = new self();
                $obj->reading_id = $reading_id;
                $obj->book_id = $val;
                $obj->save();
            }
        }
        return true;
    }


    /**
     * 删除系统类型作品
     * @param reading_id int 书单id
     * @param reading_list_book_ids  数据id 多个逗号拼接
     */
    public function delBookHomeReadingListBook($reading_id, $reading_list_book_ids)
    {
        $reading_list_book_ids = explode(',', $reading_list_book_ids);
        return $this->where('reading_id', $reading_id)->whereIn('id', $reading_list_book_ids)->delete();
    }
    /**
     * 删除系统类型作品
     * @param reading_id int 书单id
     */
    public function getBookHomeReadingListBookId($reading_id)
    {
        return $this->where('reading_id', $reading_id)->pluck('book_id')->toArray();
    }
}
